Package org.jessma.hpsocket.unicode

Source Code of org.jessma.hpsocket.unicode.Client

/*
* Copyright: JessMA Open Source (ldcsaa@gmail.com)
*
* Version  : 3.2.3
* Author  : Bruce Liang
* Website  : http://www.jessma.org
* Project  : https://github.com/ldcsaa
* Blog    : http://www.cnblogs.com/ldcsaa
* Wiki    : http://www.oschina.net/p/hp-socket
* QQ Group  : 75375912
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jessma.hpsocket.unicode;

import org.jessma.hpsocket.Buffer;
import org.jessma.hpsocket.Constant;
import org.jessma.hpsocket.SocketAddress;
import org.jessma.hpsocket.Callback.OnClose;
import org.jessma.hpsocket.Callback.OnConnect;
import org.jessma.hpsocket.Callback.OnError;
import org.jessma.hpsocket.Callback.OnPrepareConnect;
import org.jessma.hpsocket.Callback.OnPullReceive;
import org.jessma.hpsocket.Callback.OnReceive;
import org.jessma.hpsocket.Callback.OnSend;

import com.sun.jna.Native;
import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.WString;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.ptr.ShortByReference;

/** Client 组件基类 (Unicode) */
public abstract class Client extends HPSocketObj
{
  /* Client 回调函数设置方法 */
 
  /** 设置 {@linkplain OnPrepareConnect} 回调函数对象 */
  public void setCallBackOnPrepareConnect(OnPrepareConnect fn)
  {
    HPSocket.SDK.HP_Set_FN_Client_OnPrepareConnect(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnConnect} 回调函数对象 */
  public void setCallBackOnConnect(OnConnect fn)
  {
    onConnectImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnConnect(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnSend} 回调函数对象 */
  public void setCallBackOnSend(OnSend fn)
  {
    onSendImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnSend(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnReceive} 回调函数对象 */
  public void setCallBackOnReceive(OnReceive fn)
  {
    onReceiveImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnReceive(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnPullReceive} 回调函数对象 */
  public void setCallBackOnPullReceive(OnPullReceive fn)
  {
    onPullReceiveImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnPullReceive(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnClose} 回调函数对象 */
  public void setCallBackOnClose(OnClose fn)
  {
    onCloseImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnClose(socketListener, fn);
  }
 
  /** 设置 {@linkplain OnError} 回调函数对象 */
  public void setCallBackOnError(OnError fn)
  {
    onErrorImpl = fn;
    HPSocket.SDK.HP_Set_FN_Client_OnError(socketListener, fn);
  }
 
  /* ******************************** Client 组件操作方法 ********************************** */

  /**
  * 启动通信组件
  */
  public boolean start(String pszRemoteAddress, short usPort, boolean bAsyncConnect)
  {
    WString pwszRemoteAddress = pszRemoteAddress != null ? new WString(pszRemoteAddress) : null;
    return HPSocket.SDK.HP_Client_Start(socketObj, pwszRemoteAddress, usPort, bAsyncConnect);
  }

  /**
  * 关闭通信组件
  */
  public boolean stop()
  {
    return HPSocket.SDK.HP_Client_Stop(socketObj);
  }

  /**
  * 发送数据
  */
  public boolean send(byte[] pBuffer)
  {
    return HPSocket.SDK.HP_Client_Send(socketObj, pBuffer, pBuffer.length);
  }

  /**
  * 发送数据
  */
  public boolean send(Pointer pBuffer, int iLength)
  {
    return HPSocket.SDK.HP_Client_Send(socketObj, pBuffer, iLength);
  }

  /**
  * 发送指定起始位置的数据
  */
  public boolean sendPart(NativeLong dwConnID, byte[] pBuffer, int iLength, int iOffset)
  {
    return HPSocket.SDK.HP_Client_SendPart(socketObj, pBuffer, iLength, iOffset);
  }

  /**
  * 发送指定起始位置的数据
  */
  public boolean sendPart(NativeLong dwConnID, Pointer pBuffer, int iLength, int iOffset)
  {
    return HPSocket.SDK.HP_Client_SendPart(socketObj, pBuffer, iLength, iOffset);
  }

  /**
   * 发送多组数据
   */
  public boolean sendPackets(byte[][] arrays)
  {
    return sendPackets(Buffer.fromByteArrays(arrays));
  }

  /**
   * 发送多组数据
   */
  public boolean sendPackets(Buffer[] bufs)
  {
    int count     = bufs.length;
    long ptr     = Native.malloc(count * Buffer.NATIVE_BUFFER_SIZE);
    Pointer pBuffers = new Pointer(ptr);
   
    for(int i = 0; i < count; i++)
    {
      int offset = i * Buffer.NATIVE_BUFFER_SIZE;
      pBuffers.setInt(offset, bufs[i].getLength());
      pBuffers.setPointer(offset + Pointer.SIZE, bufs[i].getPointer());
    }
   
    boolean result = HPSocket.SDK.HP_Client_SendPackets(socketObj, pBuffers, count);
   
    Native.free(ptr);
    return result;
  }

  /* ******************************** Client 属性访问方法 ********************************** */

  /** 检查通信组件是否已启动 */
  public boolean hasStarted()
  {
    return HPSocket.SDK.HP_Client_HasStarted(socketObj);
  }
 
  /** 查看通信组件当前状态 */
  public int getState()
  {
    return HPSocket.SDK.HP_Client_GetState(socketObj);
  }
 
  /** 获取最近一次失败操作的错误代码 */
  public int getLastError()
  {
    return HPSocket.SDK.HP_Client_GetLastError(socketObj);
  }
 
  /** 获取最近一次失败操作的错误描述 */
  public String getLastErrorDesc()
  {
    WString desc = HPSocket.SDK.HP_Client_GetLastErrorDesc(socketObj);
    return desc != null ? desc.toString() : null;
  }
 
  /** 获取该组件对象的连接 ID */
  public NativeLong getConnectionID()
  {
    return HPSocket.SDK.HP_Client_GetConnectionID(socketObj);
  }
 
  /** 获取 Client Socket 的地址信息 */
  public SocketAddress getLocalAddress()
  {
    final int ADDR_LEN      = 40 * Constant.WCHAR_SIZE;
    byte[] lpszAddress      = new byte[ADDR_LEN];
    IntByReference piAddressLen  = new IntByReference(ADDR_LEN);
    ShortByReference pusPort  = new ShortByReference();
   
    boolean isOK = HPSocket.SDK.HP_Client_GetLocalAddress(socketObj, lpszAddress, piAddressLen, pusPort);
    return SocketAddress.craete(isOK, lpszAddress, piAddressLen, pusPort, true);
  }

  /** 获取连接中未发出数据的长度 */
  public boolean getPendingDataLength(IntByReference piPending)
  {
    return HPSocket.SDK.HP_Client_GetPendingDataLength(socketObj, piPending);
  }
 
  /** 设置内存块缓存池大小(通常设置为 -> PUSH 模型:5 - 10;PULL 模型:10 - 20 ) */
  public void setFreeBufferPoolSize(int dwFreeBufferPoolSize)
  {
    HPSocket.SDK.HP_Client_SetFreeBufferPoolSize(socketObj, dwFreeBufferPoolSize);
  }
 
  /** 设置内存块缓存池回收阀值(通常设置为内存块缓存池大小的 3 倍) */
  public void setFreeBufferPoolHold(int dwFreeBufferPoolHold)
  {
    HPSocket.SDK.HP_Client_SetFreeBufferPoolHold(socketObj, dwFreeBufferPoolHold);
  }
 
  /** 获取内存块缓存池大小 */
  public int getFreeBufferPoolSize()
  {
    return HPSocket.SDK.HP_Client_GetFreeBufferPoolSize(socketObj);
  }
 
  /** 获取内存块缓存池回收阀值 */
  public int getFreeBufferPoolHold()
  {
    return HPSocket.SDK.HP_Client_GetFreeBufferPoolHold(socketObj);
  }

}
TOP

Related Classes of org.jessma.hpsocket.unicode.Client

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.